#coding=utf-8
from stscraper import *
from stutils import *
set_django_environ()
from stdj.stock.models import *

def get_tdchange(code):
    return get_rtprice(code)[0][3]

'''
for s in Share.objects.order_by('code'):
    try:
        c = get_tdchange(s.code)
    except NoLivePriceNowError,e:
        #print e
        continue
    
    try:
        ed = s.exday_set.get(date='2007-12-21')
    except Exday.DoesNotExist:
        print s, "No data which should have."
        continue
    else: 
        c2 = str(ed.price.change)
        if c != c2:
            print s, c, c2
'''

fn = '/home/jay/temp/change_2008-01-22.txt'
data = []

for s in Share.objects.order_by('code'):
    try:
        code = s.code
        #if code == '600600':continue
        c = get_tdchange(s.code)
    except NoLivePriceNowError,e:
        #print e
        continue    
    print s.code, c
    data.append([s.code, c])
    
data2csv(data, ['代码', '涨幅'], fn)

'''
fn = '/home/jay/change_2007-01-22.txt'
data = csv2data(fn)
for d in data:
    code,c = d
    s = Share.objects.get(code=code)
    try:
        change = str(s.exday_set.get(date = '2007-12-26').price.change)
    except Exday.DoesNotExist, e:
        print s, '缺失.'
    else:
        if c != change:
            print s
            print "sohu:", c
            print 'db', change 
'''